package com.mingqi.enetconnected.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mingqi.enetconnected.view.Dialog.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作用：基类，公共类
 * Created by li on 2018/7/30.
 * WeChat 18571658038
 * author LiWei
 */
public abstract class BaseFragment extends Fragment {
    /**
     * 上下文
     */
    protected Context mContext;
    protected BaseFragment mFragment;

    private LoadingDialog mLoadingDialog;
    Unbinder unbinder;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mFragment = this;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = bindView(container);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    protected abstract View bindView(ViewGroup container);


    /**
     * 强制子类重写，实现子类特有的ui
     * @return
     */
    protected abstract void initView();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        //初始化进度提示框
        if(mLoadingDialog == null){

            mLoadingDialog =new LoadingDialog.Builder(mContext)
                    .setMessage("加载中...")
                    .setCancelable(false)
                    .create();
        }
        initView();
        initData();
        initClick();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    protected void showProDialogHint(){

        if(mLoadingDialog.isShowing()){
            return;
        } else {

            mLoadingDialog.show();
        }
    }

    protected void hideProDialogHint(){

        if(mLoadingDialog != null){

            if(mLoadingDialog.isShowing()) {

                mLoadingDialog.dismiss();
            }
        }
    }

    /**
     * 当孩子需要设置点击时间时可以重写该方法
     */
    protected void initClick() {

    }

    /**
     * 当孩子需要初始化数据，或者联网请求绑定数据，展示数据的 等等可以重写该方法
     */
    protected void initData() {

    }

    protected void startActivity(Class aClass){

        startActivity(aClass,null);
    }

    protected void startActivity(Class aClass,Bundle bundle){

        Intent intent = new Intent(mContext, aClass);
        if(bundle != null) {

            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
}
